package com.bbx.demo.socket.client;

import lombok.extern.slf4j.Slf4j;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

@Slf4j
public class SocketClient {

    public static void main (String[] args){
        Socket socket = null;
        OutputStream outputStream = null;
        InputStream inputStream = null;
        try {
            socket = new Socket(InetAddress.getByName("localhost"), 8888);
            //发送消息
            outputStream = socket.getOutputStream();
             inputStream = new FileInputStream("C:\\Users\\kaifacs\\Downloads\\SCBH201105_20201111.zip");
            //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            log.info("文件输入完毕");
            byte[] bytes1 = new byte[100];
            while(inputStream.read(bytes1)!= -1){
                outputStream.write(bytes1);
            }
            log.info("文件输出完毕");


             //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

            //显式的告诉服务端我已经输出完毕，因为InputStream.read()   方法是阻塞式的，
//            socket.shutdownOutput();
//            log.info("关闭");

            Thread.sleep(5*60000);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //关闭连接
            if (outputStream!= null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }


}
